/**
 * @author Ahammer
 * @date   2009 04
 * @update 2009 06 newWidth and newHeight values editable, image stack choice
 * @update 2009 06 new parameters ZoomX, ZoomY, ZoomOrsize
 * @update 2010 06 fixed button visibility failure (/5 and /10)
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.InternationalFormatter;
import main.IqmTools;
import main.Tank;

public class IqmResizeGUI extends IqmOperatorJFrame implements IqmOperatorGUIBuilder,
															   ActionListener, ChangeListener {

	private  ParameterBlockJAI     pbJAI       = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame    opGUIFrame  = null;
	//private IqmResizeGUI         opGUIFrame  = null;  //actionPerformed events werden in dieser Klasse abgehandelt

	private float zoomX 						 = 1.0f;
	private float zoomY 						 = 1.0f;
	
	private JRadioButtonMenuItem  butt_10        = null;
	private JRadioButtonMenuItem  butt_5         = null;
	private JRadioButtonMenuItem  butt_2         = null;  //"_"  bedeutet hier "durch"
	private JRadioButtonMenuItem  buttX2         = null;
	private JRadioButtonMenuItem  buttX5         = null;
	private JRadioButtonMenuItem  buttX10        = null;
	private JRadioButtonMenuItem  buttMagHidden  = null;
	private JToolBar              jToolBarMag    = null;
	private ButtonGroup           buttGroupMag   = null;
	
	private JRadioButtonMenuItem  buttNN         = null;	//nearest neighbor
	private JRadioButtonMenuItem  buttBL         = null;	//bilinear
	private JRadioButtonMenuItem  buttBC         = null;	//bicubic
	private JRadioButtonMenuItem  buttBC2        = null;	//bicubic2
	private JRadioButtonMenuItem  buttIntPHidden = null;
	private JToolBar              jToolBarIntP   = null;
	private ButtonGroup           buttGroupIntP  = null;
	
	private JPanel 				 	jPanelOldWidth				    = null;	
	private JLabel				 	jLabelOldWidth				    = null;
	private JFormattedTextField 	jFormattedTextFieldOldWidth     = null;	
	private JPanel 				 	jPanelOldHeight				    = null;	
	private JLabel				 	jLabelOldHeight				    = null;
	private JFormattedTextField 	jFormattedTextFieldOldHeight    = null;	
	private JPanel 				 	jPanelNewWidth		= null;	
	private JLabel				 	jLabelNewWidth		= null;
	private JSpinner			 	jSpinnerNewWidth 	= null;
	private JPanel 				 	jPanelNewHeight		= null;	
	private JLabel				 	jLabelNewHeight		= null;
	private JSpinner			 	jSpinnerNewHeight 	= null;
	
	private JRadioButtonMenuItem  buttZoom             = null;	//Preference for image stacks without same image sizes
	private JRadioButtonMenuItem  buttSize             = null;	
	private JRadioButtonMenuItem  buttZoomOrSizeHidden = null;
	private JToolBar              jToolBarZoomOrSize   = null;
	private ButtonGroup           buttGroupZoomOrSize  = null;
	private JPanel			      jPanelZoomOrSize     = null;
	private	JLabel				  jLabelZoomOrSize	   = null;
	
	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pbJAI
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pbJAI = arg;
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 */
	@Override
	public void setParameterBlock(){
		pbJAI.setParameter("ZoomX", zoomX);
		pbJAI.setParameter("ZoomY", zoomY);
		
		pbJAI.setParameter("NewWidth",  ((Number)jSpinnerNewWidth.getValue()).intValue());
		pbJAI.setParameter("NewHeight", ((Number)jSpinnerNewHeight.getValue()).intValue());
		
		if (buttNN.isSelected())  pbJAI.setParameter("Interpolation", 0);
		if (buttBL.isSelected())  pbJAI.setParameter("Interpolation", 1);
		if (buttBC.isSelected())  pbJAI.setParameter("Interpolation", 2);
		if (buttBC2.isSelected()) pbJAI.setParameter("Interpolation", 3);
		
		if (buttZoom.isSelected()) pbJAI.setParameter("ZoomOrSize", 0);
		if (buttSize.isSelected()) pbJAI.setParameter("ZoomOrSize", 1);
	    
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	
	/**
	 * This method sets the current parameter values
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmResizeGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
//      String type = IqmTools.getCurrImgTyp(pi);

		buttMagHidden.setSelected(true);	
		buttIntPHidden.setSelected(true);
		buttZoomOrSizeHidden.setSelected(true);
		
		zoomX = pbJAI.getFloatParameter("ZoomX");
		zoomY = pbJAI.getFloatParameter("ZoomY");
		
	    jSpinnerNewWidth.removeChangeListener(this);	
	    jSpinnerNewHeight.removeChangeListener(this);    
	    jSpinnerNewWidth.setValue(pbJAI.getIntParameter("NewWidth"));
	    jSpinnerNewHeight.setValue(pbJAI.getIntParameter("NewHeight"));
	    jSpinnerNewWidth.addChangeListener(this);
	    jSpinnerNewHeight.addChangeListener(this);
	    	    
		if (new Float(zoomX).equals(new Float(zoomY))){
			if (Math.round(1.0f/zoomX) == 10.0f) butt_10.setSelected(true); //ensures button appearance 
			if (Math.round(1.0f/zoomX) == 5.0f)  butt_5.setSelected(true);  //ensures button appearance
			if (zoomX == (1.0f/2.0f))            butt_2.setSelected(true);
			if (zoomX == 2.0f)    buttX2.setSelected(true);
			if (zoomX == 5.0f)    buttX5.setSelected(true);
			if (zoomX == 10.0f)   buttX10.setSelected(true);
		}
		if (pbJAI.getIntParameter("Interpolation") == 0)  buttNN.setSelected(true);
		if (pbJAI.getIntParameter("Interpolation") == 1)  buttBL.setSelected(true);
		if (pbJAI.getIntParameter("Interpolation") == 2)  buttBC.setSelected(true);
		if (pbJAI.getIntParameter("Interpolation") == 3)  buttBC2.setSelected(true);
		
		if (pbJAI.getIntParameter("ZoomOrSize") == 0)  buttZoom.setSelected(true);
		if (pbJAI.getIntParameter("ZoomOrSize") == 1)  buttSize.setSelected(true);

	}
	/**
	 * constructor
	 */
	public IqmResizeGUI() {

	}
	/**
	 * This method creates and shows the GUI
	 * 
	 */
	@Override
	public void createIqmOperatorGUI() {
		//opGUIFrame = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame = new IqmResizeGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("Resize");  
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height+150);
		this.getJContentPane().add(getJToolBarMag(),        getGridBagConstraintsButtonGroupMag());
		this.getJContentPane().add(getJToolBarIntP(),       getGridBagConstraintsButtonGroupIntP());
		this.getJContentPane().add(getJPanelOldWidth(),     getGridBagConstraintsOldWidth());
		this.getJContentPane().add(getJPanelOldHeight(),    getGridBagConstraintsOldHeight());
		this.getJContentPane().add(getJPanelNewWidth(),     getGridBagConstraintsNewWidth());
		this.getJContentPane().add(getJPanelNewHeight(),    getGridBagConstraintsNewHeight());
		//this.getJContentPane().add(getJToolBarZoomOrSize(), getGridBagConstraintsButtonGroupZoomOrSize());
		this.getJContentPane().add(getJPanelZoomOrSize(),   getGridBagConstraintsZoomOrSize());

		//this.repaint();
		if (pbJAI == null) pbJAI = new ParameterBlockJAI("IqmResize");  //nur wenn das erste mal aufgerufen
		//pbJAI = new ParameterBlockJAI("IqmResize"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pbJAI);
		this.setParameterValues();
		this.setOldValues();
		this.setNewWidthHeight();
		this.setParameterBlock();
		this.pack();
	}
	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	public void updateIqmOperatorGUI(){
		this.setOldValues();
		this.setNewWidthHeight();
		//this.pack();
	}
	/**
	 * This method displays the old values
	 */
	private  void setOldValues(){
	   	PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
	   	jFormattedTextFieldOldWidth.setValue(pi.getWidth()); 
	   	jFormattedTextFieldOldHeight.setValue(pi.getHeight());	
	}
	/**
	 * This method displays the new values 
	 */
	private  void setNewWidthHeight(){
    	PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
    	int[] newValues = calcNewWidthHeight(zoomX, zoomY, pi.getWidth(), pi.getHeight());
	    jSpinnerNewWidth.removeChangeListener(this);	
	    jSpinnerNewHeight.removeChangeListener(this);    
		jSpinnerNewWidth.setValue(newValues[0]);
		jSpinnerNewHeight.setValue(newValues[1]);
	    jSpinnerNewWidth.addChangeListener(this);
	    jSpinnerNewHeight.addChangeListener(this);
		
	}
	/**
	 * This method calculates the new values
	 * @param int optZoom pressed button int argX old x value, int argY old y value
	 * @return int[] the new x and y value
	 */
	private int[] calcNewWidthHeight(float zoomX, float zoomY, int argX, int argY){
		//calculate new size
		int result[] = new int[2];
		result[0] = Math.round((float)argX*zoomX);
		result[1] = Math.round((float)argY*zoomY);
		return result;
	}
	private GridBagConstraints getGridBagConstraintsButtonGroupMag(){
		GridBagConstraints gridBagConstraintsButtonMagGroup = new GridBagConstraints();
		gridBagConstraintsButtonMagGroup.gridx = 1;
		gridBagConstraintsButtonMagGroup.gridy = 4;
		//gridBagConstraintsButtonMagGroup.gridwidth = 3;//?
		//gridBagConstraintsButtonMagGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonMagGroup;
	}
	private GridBagConstraints getGridBagConstraintsButtonGroupIntP(){
		GridBagConstraints gridBagConstraintsButtonIntPGroup = new GridBagConstraints();
		gridBagConstraintsButtonIntPGroup.gridx = 2;
		gridBagConstraintsButtonIntPGroup.gridy = 4;
		gridBagConstraintsButtonIntPGroup.gridwidth = 2;//?
		//gridBagConstraintsButtonIntPGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonIntPGroup;
	}
	private GridBagConstraints getGridBagConstraintsOldWidth(){
		GridBagConstraints gridBagConstraintsOldWidth = new GridBagConstraints();
		gridBagConstraintsOldWidth.gridx = 1;
		gridBagConstraintsOldWidth.gridy = 5;
		gridBagConstraintsOldWidth.gridwidth = 1;//?
		gridBagConstraintsOldWidth.insets = new Insets(10,0,0,0); //top left bottom right
		//gridBagConstraintsOldWidth.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOldWidth;
	}
	private GridBagConstraints getGridBagConstraintsOldHeight(){
		GridBagConstraints gridBagConstraintsOldHeight = new GridBagConstraints();
		gridBagConstraintsOldHeight.gridx = 2;
		gridBagConstraintsOldHeight.gridy = 5;
		gridBagConstraintsOldHeight.gridwidth = 2;//?
		gridBagConstraintsOldHeight.insets = new Insets(10,20,0,0);  //top left bottom right
		//gridBagConstraintsOldHeight.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsOldHeight;
	}

	private GridBagConstraints getGridBagConstraintsNewWidth(){
		GridBagConstraints gridBagConstraintsNewWidth = new GridBagConstraints();
		gridBagConstraintsNewWidth.gridx = 1;
		gridBagConstraintsNewWidth.gridy = 7;
		gridBagConstraintsNewWidth.gridwidth = 1;//?
		gridBagConstraintsNewWidth.insets = new Insets(10,0,20,0);  //top left bottom right
		//gridBagConstraintsNewWidth.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNewWidth;
	}
	private GridBagConstraints getGridBagConstraintsNewHeight(){
		GridBagConstraints gridBagConstraintsNewHeight = new GridBagConstraints();
		gridBagConstraintsNewHeight.gridx = 2;
		gridBagConstraintsNewHeight.gridy = 7;
		gridBagConstraintsNewHeight.gridwidth = 2;//?
		gridBagConstraintsNewHeight.insets = new Insets(10,20,20,0);  //top left bottom right
		//gridBagConstraintsNewHeight.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNewHeight;
	}
	private GridBagConstraints getGridBagConstraintsZoomOrSize(){
		GridBagConstraints gridBagConstraintsButtonZoomOrSizeGroup = new GridBagConstraints();
		gridBagConstraintsButtonZoomOrSizeGroup.gridx = 1;
		gridBagConstraintsButtonZoomOrSizeGroup.gridy = 8;
		gridBagConstraintsButtonZoomOrSizeGroup.gridwidth = 3;//?
		//gridBagConstraintsButtonZoomOrSizeGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonZoomOrSizeGroup;
	}
	//------------------------------------------------------------------------------------------------------

	
	/**
	 * This method initializes the Option: /10		
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButt_10() {
		//if (butt_10 == null) {
			butt_10 = new JRadioButtonMenuItem();
			butt_10.setText("/10");
			butt_10.setToolTipText("resizes to 1/10");
			butt_10.addActionListener(this);
			butt_10.setActionCommand("parameter");
		//}
		return butt_10;
	}
	/**
	 * This method initializes the Option: /5	 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButt_5() {
		//if (butt_5 == null) {
			butt_5 = new JRadioButtonMenuItem();
			butt_5.setText("/5");
			butt_5.setToolTipText("resizes to 1/5");
			butt_5.addActionListener(this);
			butt_5.setActionCommand("parameter");
		//}
		return butt_5;
	}
	/**
	 * This method initializes the Option: /2 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButt_2() {
		//if (butt_2 == null) {
			butt_2 = new JRadioButtonMenuItem();
			butt_2.setText("/2");
			butt_2.setToolTipText("resizes to 1/2");
			butt_2.addActionListener(this);
			butt_2.setActionCommand("parameter");
		//}
		return butt_2;
	}
	/**
	 * This method initializes the Option: 2 times 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtX2() {
		//if (buttX2 == null) {
			buttX2  = new JRadioButtonMenuItem();
			buttX2 .setText("x2");
			buttX2 .setToolTipText("resizes 2 times");
			buttX2 .addActionListener(this);
			buttX2 .setActionCommand("parameter");
		//}
		return buttX2;
	}
	/**
	 * This method initializes the Option: 5 times	 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtX5() {
		//if (buttX5 == null) {
			buttX5  = new JRadioButtonMenuItem();
			buttX5 .setText("x5");
			buttX5 .setToolTipText("resizes 5 times");
			buttX5 .addActionListener(this);
			buttX5 .setActionCommand("parameter");
		//}
		return buttX5;
	}
	/**
	 * This method initializes the Option: 10 times		
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtX10() {
		//if (buttX10 == null) {
			buttX10  = new JRadioButtonMenuItem();
			buttX10 .setText("x10");
			buttX10 .setToolTipText("resizes 10 times");
			buttX10 .addActionListener(this);
			buttX10.setActionCommand("parameter");
		//}
		return buttX10;
	}

	/**
	 * This method initializes the hidden option	 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtMagHidden() {
		//if (buttMagHidden == null) {
			buttMagHidden  = new JRadioButtonMenuItem();
			buttMagHidden .setText("Hidden");
			buttMagHidden.setVisible(false);
		//}
		return buttMagHidden;
	}
	/**
	 * This method initializes jJToolBarBar	 	
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getJToolBarMag() {
//		if (jToolBarMag == null) {
			jToolBarMag = new JToolBar("Options");
			jToolBarMag.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarMag.add(getJRadioButtonMenuButt_10());
			jToolBarMag.add(getJRadioButtonMenuButt_5());
			jToolBarMag.add(getJRadioButtonMenuButt_2());
			jToolBarMag.add(getJRadioButtonMenuButtX2());			
			jToolBarMag.add(getJRadioButtonMenuButtX5());
			jToolBarMag.add(getJRadioButtonMenuButtX10());
			jToolBarMag.add(getJRadioButtonMenuButtMagHidden());
			//jToolBarMag.addSeparator();
			this.setButtonGroupMag();	//Gruppieren der JRadioButtons
//		}
		return jToolBarMag;
	}
	private void setButtonGroupMag() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupMag = new ButtonGroup();
			buttGroupMag.add(butt_10);
			buttGroupMag.add(butt_5);
			buttGroupMag.add(butt_2);
			buttGroupMag.add(buttX2);
			buttGroupMag.add(buttX5);
			buttGroupMag.add(buttX10);
			buttGroupMag.add(buttMagHidden);
	}
	//-------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: Nearest Neighbor		
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtNN() {
		//if (buttNN == null) {
			buttNN  = new JRadioButtonMenuItem();
			buttNN .setText("Nearest Neighbor");
			buttNN .setToolTipText("uses Nearest Neighbor resampling for resizing");
			buttNN .addActionListener(this);
			buttNN.setActionCommand("parameter");
		//}
		return buttNN;
	}
	/**
	 * This method initializes the Option: Bilinear interpolation	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtBL() {
		//if (buttBL == null) {
			buttBL  = new JRadioButtonMenuItem();
			buttBL .setText("Bilinear");
			buttBL .setToolTipText("uses Bilinear interpolation for resizing");
			buttBL .addActionListener(this);
			buttBL.setActionCommand("parameter");
		//}
		return buttBL;
	}
	/**
	 * This method initializes the Option: Bicubic interpolation	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtBC() {
		//if (buttBC == null) {
			buttBC  = new JRadioButtonMenuItem();
			buttBC .setText("Bicubic");
			buttBC .setToolTipText("uses Bicubic interpoolation for resizing");
			buttBC .addActionListener(this);
			buttBC.setActionCommand("parameter");
		//}
		return buttBC;
	}
	/**
	 * This method initializes the Option: Bicubic2 interpolation	 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtBC2() {
		//if (buttBC2 == null) {
			buttBC2  = new JRadioButtonMenuItem();
			buttBC2 .setText("Bicubic2");
			buttBC2 .setToolTipText("uses Bicubic2 interpolation for resizing");
			buttBC2 .addActionListener(this);
			buttBC2.setActionCommand("parameter");
		//}
		return buttBC2;
	}
	
	/**
	 * This method initializes the hidden option	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private JRadioButtonMenuItem getJRadioButtonMenuButtIntPHidden() {
		//if (buttIntPHidden == null) {
			buttIntPHidden  = new JRadioButtonMenuItem();
			buttIntPHidden .setText("Hidden");
			buttIntPHidden.setVisible(false);
		//}
		return buttIntPHidden;
	}


	/**
	 * This method initializes jJToolBarBar		
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getJToolBarIntP() {
//		if (jToolBarIntP== null) {
			jToolBarIntP = new JToolBar("Options");
			jToolBarIntP.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarIntP.add(getJRadioButtonMenuButtNN());
			jToolBarIntP.add(getJRadioButtonMenuButtBL());
			jToolBarIntP.add(getJRadioButtonMenuButtBC());
			jToolBarIntP.add(getJRadioButtonMenuButtBC2());
			jToolBarIntP.add(getJRadioButtonMenuButtIntPHidden());
			//jToolBarIntP.addSeparator();
			this.setButtonGroupIntP();	//Gruppieren der JRadioButtons
//		}
		return jToolBarIntP;
	}

	private void setButtonGroupIntP() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupIntP = new ButtonGroup();
			buttGroupIntP.add(buttNN);
			buttGroupIntP.add(buttBL);
			buttGroupIntP.add(buttBC);
			buttGroupIntP.add(buttBC2);
			buttGroupIntP.add(buttIntPHidden);
	}
	//----------------------------------------------------------------------------------------------------------

	class IntNumberVerifier extends InputVerifier { //damit muss Eingabe richtig sein 
		@Override
		public boolean verify(JComponent input) {
		   JFormattedTextField ftf = (JFormattedTextField)input;
		   JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter();
		   if (formatter != null) {
		      String text = ftf.getText();
		  	  try {
				   text = text.replace(",", ".");
				   Integer.valueOf(text);
				   //Float.valueOf(text);
					return true;
			  } catch (NumberFormatException e) {
					return false;
			  }	 
		   }
		   return true;
		}
//		public boolean shouldYieldFocus(JComponent input) {
//		System.out.println("NumberVerifier  shouldYieldFocus");
//
//          return verify(input);
//    }
}
	/**
	 * This method initializes jJPanelOldWidth		
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelOldWidth() {
		if (jPanelOldWidth == null) {
			jPanelOldWidth = new JPanel();
			jPanelOldWidth.setLayout(new BorderLayout());
			//jPanelOldWidth.setPreferredSize(new Dimension(250,18));					
			jLabelOldWidth = new JLabel("Old Width: ");
			jLabelOldWidth.setPreferredSize(new Dimension(65,18));
			jLabelOldWidth.setHorizontalAlignment(JLabel.RIGHT);
			jFormattedTextFieldOldWidth = new JFormattedTextField(NumberFormat.getNumberInstance());
			jFormattedTextFieldOldWidth.setPreferredSize(new Dimension(70,18));
			//jFormattedTextFieldOldWidth.addPropertyChangeListener("value", this);
			//jFormattedTextFieldOldWidth.setInputVerifier(new IntNumberVerifier());
			jFormattedTextFieldOldWidth.setEditable(false);
			//jFormattedTextFieldOldWidth.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);	
			jFormattedTextFieldOldWidth.setColumns(5);
			InternationalFormatter intFormatter = (InternationalFormatter)jFormattedTextFieldOldWidth.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			jPanelOldWidth.add(jLabelOldWidth, 			    BorderLayout.WEST);
			jPanelOldWidth.add(jFormattedTextFieldOldWidth, BorderLayout.CENTER);	
		
		}
		return jPanelOldWidth;
	}
	/**
	 * This method initializes jJPanelOldHeight	 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelOldHeight() {
		if (jPanelOldHeight == null) {
			jPanelOldHeight = new JPanel();
			jPanelOldHeight.setLayout(new BorderLayout());			
			jLabelOldHeight = new JLabel("Old Height: ");
			jLabelOldHeight.setPreferredSize(new Dimension(65,18));
			jLabelOldHeight.setHorizontalAlignment(JLabel.RIGHT);
			jFormattedTextFieldOldHeight = new JFormattedTextField(NumberFormat.getNumberInstance());
			jFormattedTextFieldOldHeight.setPreferredSize(new Dimension(70,18));
			//jFormattedTextFieldOldHeight.addPropertyChangeListener("value", this);
			//jFormattedTextFieldOldHeight.setInputVerifier(new IntNumberVerifier());
			jFormattedTextFieldOldHeight.setEditable(false);
			//jFormattedTextFieldOldHeight.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);	
			jFormattedTextFieldOldHeight.setColumns(5);
			InternationalFormatter intFormatter = (InternationalFormatter)jFormattedTextFieldOldHeight.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;
			jPanelOldHeight.add(jLabelOldHeight, 			  BorderLayout.WEST);
			jPanelOldHeight.add(jFormattedTextFieldOldHeight, BorderLayout.CENTER);
			jFormattedTextFieldOldHeight.setEnabled(true);	
		}
		return jPanelOldHeight;
	}

	/**
	 * This method initializes jJPanelNewWidth	 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNewWidth() {
		if (jPanelNewWidth == null) {
			jPanelNewWidth = new JPanel();
			jPanelNewWidth.setLayout(new BorderLayout());		
			jLabelNewWidth = new JLabel("New Width: ");
			jLabelNewWidth.setPreferredSize(new Dimension(70, 22));
			jLabelNewWidth.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNewWidth = new JSpinner(sModel);
			jSpinnerNewWidth.setPreferredSize(new Dimension(60, 22));
			jSpinnerNewWidth.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNewWidth.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNewWidth.add(jLabelNewWidth, 	 BorderLayout.WEST);
			jPanelNewWidth.add(jSpinnerNewWidth, BorderLayout.CENTER);			
		}
		return jPanelNewWidth;
	}
	/**
	 * This method initializes jJPanelNewHeight		
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNewHeight() {
		if (jPanelNewHeight == null) {
			jPanelNewHeight = new JPanel();
			jPanelNewHeight.setLayout(new BorderLayout());		
			jLabelNewHeight = new JLabel("New Height: ");
			jLabelNewHeight.setHorizontalAlignment(JLabel.RIGHT);
			jLabelNewHeight.setPreferredSize(new Dimension(70, 22));
			SpinnerModel sModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNewHeight = new JSpinner(sModel);
			jSpinnerNewHeight.setPreferredSize(new Dimension(60, 22));
			jSpinnerNewHeight.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNewHeight.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNewHeight.add(jLabelNewHeight,   BorderLayout.WEST);
			jPanelNewHeight.add(jSpinnerNewHeight, BorderLayout.CENTER);			
		}
		return jPanelNewHeight;
	}
	//--------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtZoom() {
		//if (buttZoom == null) {
			buttZoom  = new JRadioButtonMenuItem();
			buttZoom .setText("Zoom");
			buttZoom .setToolTipText("prefers the zoom setting instead of the new size values");
			buttZoom .addActionListener(this);
			buttZoom.setActionCommand("parameter");
		//}
		return buttZoom;
	}
	/**
	 * This method initializes the Option: 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtSize() {
		//if (buttSize == null) {
			buttSize  = new JRadioButtonMenuItem();
			buttSize .setText("New Size");
			buttSize .setToolTipText("prefers the new size setting instead of the zoom values");
			buttSize .addActionListener(this);
			buttSize.setActionCommand("parameter");
		//}
		return buttSize;
	}

	/**
	 * This method initializes the hidden option		
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtZoomOrSizeHidden() {
		//if (buttZoomOrSizeHidden == null) {
			buttZoomOrSizeHidden  = new JRadioButtonMenuItem();
			buttZoomOrSizeHidden .setText("Hidden");
			buttZoomOrSizeHidden.setVisible(false);
		//}
		return buttZoomOrSizeHidden;
	}


//	/**
//	 * This method initializes jJToolBarBar	
//	 * @return javax.swing.JToolBar	
//	 */
//	private   JToolBar getJToolBarZoomOrSize() {
////		if (jToolBarZoomOrSize== null) {
//			jToolBarZoomOrSize = new JToolBar("Options");
//			jToolBarZoomOrSize.setOrientation(1); // 0 Horizontal 1 Vertical orientation
//			jToolBarZoomOrSize.add(getJRadioButtonMenuButtZoom());
//			jToolBarZoomOrSize.add(getJRadioButtonMenuButtSize());
//			jToolBarZoomOrSize.add(getJRadioButtonMenuButtZoomOrSizeHidden());
//			//jToolBarZoomOrSize.addSeparator();
//			this.setButtonGroupZoomOrSize();	//Gruppieren der JRadioButtons
////		}
//		return jToolBarZoomOrSize;
//	}
	/**
	 * This method initializes jJToolBarBar		
	 * @return javax.swing.JToolBar	
	 */
	private JPanel getJPanelZoomOrSize() {
//		if (jToolBarZoomOrSize== null) {
			jPanelZoomOrSize = new JPanel();
			jPanelZoomOrSize.setLayout(new BorderLayout());	
			jLabelZoomOrSize = new JLabel("Preference if image size varies in a stack : ");
			jToolBarZoomOrSize = new JToolBar("Options");
			jToolBarZoomOrSize.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarZoomOrSize.add(getJRadioButtonMenuButtZoom());
			jToolBarZoomOrSize.add(getJRadioButtonMenuButtSize());
			jToolBarZoomOrSize.add(getJRadioButtonMenuButtZoomOrSizeHidden());
			//jToolBarZoomOrSize.addSeparator();
			this.setButtonGroupZoomOrSize();	//Gruppieren der JRadioButtons
			jPanelZoomOrSize.add(jLabelZoomOrSize, 	 BorderLayout.WEST);
			jPanelZoomOrSize.add(jToolBarZoomOrSize, BorderLayout.CENTER);			
//		}
		return jPanelZoomOrSize;
	}


	private void setButtonGroupZoomOrSize() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupZoomOrSize = new ButtonGroup();
			buttGroupZoomOrSize.add(buttZoom);
			buttGroupZoomOrSize.add(buttSize);
			buttGroupZoomOrSize.add(buttZoomOrSizeHidden);
	}
    //----------------------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmResizeGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pbJAI = new ParameterBlockJAI("IqmResize".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pbJAI);
			this.setParameterBlock(pbJAI);
			this.setParameterValues();
			//this.setOldValues();
			//this.setNewWidthHeight();
			this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {		
			if(butt_10 == e.getSource()){
				zoomX = 1.0f/10.0f;
				zoomY = 1.0f/10.0f;
			}
			if(butt_5  == e.getSource()){
				zoomX = 1.0f/5.0f;
				zoomY = 1.0f/5.0f;
			}
			if(butt_2  == e.getSource()){
				zoomX = 1.0f/2.0f;
				zoomY = 1.0f/2.0f;
			}
			if(buttX2  == e.getSource()){
				zoomX = 2.0f;
				zoomY = 2.0f;
			}
			if(buttX5  == e.getSource()){
				zoomX = 5.0f;
				zoomY = 5.0f;
			}
			if(buttX10 == e.getSource()){
				zoomX = 10.0f;
				zoomY = 10.0f;
			}
			//this.setOldValues(); not really necessary
			this.setParameterBlock();
			this.setParameterValues();
			//this.setNewWidthHeight();
			this.updateIqmOperatorGUI();  //if necessary here or some lines above
	
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//-------------------------------------------------------------------------------------------------
	@Override
	public void stateChanged(ChangeEvent e) {
		
		int newWidth  = ((Number)jSpinnerNewWidth.getValue()).intValue();
		int newHeight = ((Number)jSpinnerNewHeight.getValue()).intValue();
		int oldWidth  = ((Number)jFormattedTextFieldOldWidth.getValue()).intValue();
		int oldHeight = ((Number)jFormattedTextFieldOldHeight.getValue()).intValue();
		
		if (jSpinnerNewWidth  == e.getSource()) {
			zoomX = (float)newWidth/(float)oldWidth;
			zoomY = (float)newHeight/(float)oldHeight;	

		}
		if (jSpinnerNewHeight  == e.getSource()) {
			zoomX = (float)newWidth/(float)oldWidth;
			zoomY = (float)newHeight/(float)oldHeight;	
		}
		this.setParameterBlock();
		this.setParameterValues();
		this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
		
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmResizeGUI gui = new IqmResizeGUI();
			    gui.createIqmOperatorGUI();

			}
		});
	}
	
}//END
